#include <iostream>
#include <fstream>
using namespace std;
#include "TileSystem.h"
#include "BaseObject.h"
#include <vld.h>
#include "Layer.h"
#include "tinyxml.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "GameplayState.h"
#include "ObjectManager.h"
#include "StaticObject.h"
#include "GameplayState.h"
#include "Game.h"
#include "BitmapFont.h"
#include "Camera.h"
#include "EmergencyLights.h"
#include "Crate.h"
#include "Boots.h"
#include "Bomb.h"
#include "Gate.h"
#include "Scissors.h"
#include "GrappleGun.h"
#include "Phase.h"
#include "Fan.h"

struct tile
{
	int X, Y, Lay, ID, ColID, IsCol;

	std::string ColInit;
};

TileSystem::TileSystem()
{
	
}

TileSystem::~TileSystem()
{
	SGD::GraphicsManager* pGraphics = SGD::GraphicsManager::GetInstance();

	pGraphics->UnloadTexture(m_hTileSet);

	for (unsigned int i = 0; i < m_ndelete.size(); i++)
	{
		delete m_vLayerVector.at(m_ndeletelayer.at(i))->m_arrTileArray.at(m_ndelete.at(i));
	}

	for (unsigned int i = 0; i < m_vLayerVector.size(); i++)
	{
		m_vLayerVector.at(i)->m_arrTileArray.clear();
		delete m_vLayerVector.at(i);
	}

}
SGD::Rectangle TileSystem::CellAlgorithm(int id)const
{
	SGD::Rectangle cell;


	cell.left = (float)((id % m_nTileColumns) * m_nTileWidth);
	cell.top = (float)((id / m_nTileColumns) * m_nTileHeight);

	cell.right = cell.left + m_nTileWidth;
	cell.bottom = cell.top + m_nTileHeight;

	return cell;

}

void TileSystem::Update()
{
	GameplayState* game = GameplayState::GetInstance();

	// Get Camera offset
	int x = GameplayState::GetInstance()->GetCamX();
	int y = GameplayState::GetInstance()->GetCamY();

	m_nStartingX = (x / m_nTileWidth)-15;// -((Game::GetInstance()->GetScreenWidth()));
	m_nStartingY = (y / m_nTileHeight)-4;// -((Game::GetInstance()->GetScreenWidth()));


	if (m_nStartingX < 0)
	{
		m_nEndingX = (m_nStartingX)+(Game::GetInstance()->GetScreenWidth() / m_nTileWidth) + 17;
		m_nStartingX = 0;
	}
	else
		m_nEndingX = m_nStartingX + (Game::GetInstance()->GetScreenWidth() / m_nTileWidth) + 17;

	if (m_nStartingY < 0)
	{
		m_nEndingY = (m_nStartingY - 6) + (Game::GetInstance()->GetScreenWidth() / m_nTileHeight)+5;
		m_nStartingY = 0;
	}
	else
		m_nEndingY = m_nStartingY + (Game::GetInstance()->GetScreenWidth() / m_nTileHeight);


	if (m_nEndingX > m_nMapWidth)
		m_nEndingX = m_nMapWidth;
	if (m_nEndingY > m_nMapHeight)
		m_nEndingY = m_nMapHeight;
}

void TileSystem::Render()
{
	SGD::GraphicsManager* pGraphics = SGD::GraphicsManager::GetInstance();
	GameplayState* game = GameplayState::GetInstance();

	// Get Camera offset
	int x = GameplayState::GetInstance()->GetCamX();
	int y = GameplayState::GetInstance()->GetCamY();

		for (unsigned int index = 0; index < m_vLayerVector.size(); index++)
		{
			for (int i = m_nStartingX; i < m_nEndingX; i++)
			{
				for (int j = m_nStartingY; j <  m_nEndingY; j++)
				{
					// Render tile
					if (m_vLayerVector[index]->m_arrTileArray[i + j *m_nMapWidth]!=nullptr)
					pGraphics->DrawTextureSection
						(
						m_hTileSet,
						{
						(float)m_vLayerVector[index]->m_arrTileArray[i + j *m_nMapWidth]->getLocX() * m_nTileWidth - x,
						(float)m_vLayerVector[index]->m_arrTileArray[i + j *m_nMapWidth]->getLocY() * m_nTileHeight - y
					},
					CellAlgorithm(m_vLayerVector[index]->m_arrTileArray[i + j *m_nMapWidth]->getTileID())
					);
				}
			}
		}

}

std::string TileSystem::GetTileSetPath()
{
	return m_szTileSetID;
}


void TileSystem::CreateMap()
{

}


bool TileSystem::Load(std::string FileName , ObjectManager* obj)
{
	SGD::GraphicsManager* pGraphics = SGD::GraphicsManager::GetInstance();
	ObjectManager* objects = obj; //GameplayState::GetInstance()->GetObjectManager();

	int width = (int)(GameplayState::GetInstance()->GetScreenWidth()/2)- 7*32;
	int height = (int)GameplayState::GetInstance()->GetScreenHeight()/2;
	
	SGD::String Loading = L"Loading.. 0%";
	BitmapFont font;
	font.Initialize();

	font.Draw(&Loading, width, height, 1, SGD::Color());
	pGraphics->Update(); 

	fstream file;
	
	file.open(FileName, ios_base::in | ios_base::binary);
	if (file.good())
	{
		// Get Image used from file
		int TileSetIDLength;
		file.read((char*)&TileSetIDLength, sizeof(TileSetIDLength));
		m_szTileSetID.resize(TileSetIDLength);
		file.read(&m_szTileSetID[0], TileSetIDLength);

		m_hTileSet = SGD::GraphicsManager::GetInstance()->LoadTexture(m_szTileSetID.c_str());

		// Get map width and height
		file.read((char*)&m_nMapWidth, sizeof(m_nMapWidth));
		file.read((char*)&m_nMapHeight, sizeof(m_nMapHeight));


		// Get Tile Size
		file.read((char*)&m_nTileWidth, sizeof(m_nTileWidth));
		file.read((char*)&m_nTileHeight, sizeof(m_nTileHeight));

		file.read((char*)&m_nTileRows, sizeof(m_nTileRows));
		file.read((char*)&m_nTileColumns, sizeof(m_nTileColumns));

		// Get the number of tiles in the map
		long numTiles = 0;
		file.read((char*)&numTiles, sizeof(numTiles));


		// Initialize layers
		float count = 1;
		m_vLayerVector.resize(3);
		for (unsigned char j = 0; j < 3; j++)
		{
			m_vLayerVector.at(j) = new Layer;
			m_vLayerVector.at(j)->m_arrTileArray.resize(m_nMapWidth * m_nMapHeight);
		}

		objects->GetAllObjects()->resize(m_nMapWidth * m_nMapHeight);

		int X = 0; 
		int Y = 0;
		int Lay = 0;
		int ID = 0;
		bool IsCol = 0;
		int  ColID = 0;

		//fstream ff;
		//
		//
		//ff.open("resource/graphics/TestSounds.binary", std::ios_base::binary | std::ios_base::in);
		//
		//if (ff.is_open())
		//for (int i = 0; i < numTiles; i++)
		//{
		//	tile t;
		//	file.read((char*)&t, sizeof(t));
		//	if (!file.good())
		//	{
		//		std::streamsize lastp = file.gcount();
		//		file.clear();
		//		file.seekg(lastp);
		//	}
		//
		//	int temp = 0;
		//}

		for (int i = 0; i < numTiles; i++)
		{
			std::string ColInit;

			if (i % 1000 == 0)
			{
				int er = Loading.find_first_of('.');
				Loading.erase(Loading.begin() + er + 3, Loading.end());

				Loading += to_wstring((count / (numTiles)) * 100);

				er = Loading.find_last_of('.');
				Loading.erase(Loading.begin() + er + 3, Loading.end());
				Loading += L"%";
				font.Draw(&Loading, width, height, 1, SGD::Color());
				pGraphics->Update();
			}
			count++;


			file.read((char*)&X, sizeof(X));
			file.read((char*)&Y, sizeof(Y));
			file.read((char*)&Lay, sizeof(Lay));
			file.read((char*)&ID, sizeof(ID));
			file.read((char*)&IsCol, sizeof(IsCol));

			int nSizeOfColInit = 0;
			file.read((char*)&nSizeOfColInit, sizeof(nSizeOfColInit));
			ColInit.resize(nSizeOfColInit);
			file.read(&ColInit[0], nSizeOfColInit);

			if (!file.good())
			{
				int temps = 0;
			}

					Tile* tempTile;
					// If there is collision, create an object, if not, create a tile
					if (IsCol == 1)
					{
						if (ColInit == "Lights")
						{
							EmergencyLights* object = new EmergencyLights;
							object->SetSize({ 32, 64 });
							object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
							object->SetObjectType(ObjectType::GROUND);
							objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;

						}
						else if (ColInit == "GrappleHook")
						{
							GrappleGun* object = new GrappleGun();

							object->SetObjectType(ObjectType::GRAPPLEHOOK);
							object->SetSize({ 64, 64 });
							object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
							object->SetAlive(true);
							objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;

						}
						else if (ColInit == "PhaseGauntlets")
						{
							Phase* object = new Phase();

							object->SetObjectType(ObjectType::PhaseGun);
							object->SetSize({ 64, 64 });
							object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
							objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;

						}
						else if (ColInit == "Ceiling Fan")
						{
							Fan* object = new Fan();
							object->SetObjectType(ObjectType::Fan);
							object->SetSize({ 113, 64 });
							object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
							objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;
						}
						else if (ColInit == "SmashCrate")
						{
							Crate* crate = new Crate;
							crate->SetSize({128, 128});
							crate->SetPosition({(float)X * m_nTileWidth, (float)Y * m_nTileHeight});
							crate->SetObjectType(ObjectType::SmashCrate);
							objects->GetAllObjects()->at(X + Y *m_nMapWidth) = crate;
							objects->AddObjectTable(crate, 0);

							ParticleSystem ps;
							ps.Load("resource//savefiles//BoxBreaking.xml", ps, *crate);
							crate->SetParticle(ps);
							crate->SetWasTriggered(false);
						}
						else if (ColInit == "Bomb")
						{
							Bomb* bomb = new Bomb();
							bomb->SetSize({ 512, 256 });
							bomb->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
							bomb->SetObjectType(ObjectType::Bomb);
							objects->GetAllObjects()->at(X + Y *m_nMapWidth) = bomb;
							
						}
						else if (ColInit == "GravBoots")
						{
							Boots* object = new Boots;
							object->SetSize({ 64, 64 });
							object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
							object->SetObjectType(ObjectType::GravBoots);
							objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;
						}
						else if (ColInit == "Gate")
						{
							Gate* object = new Gate;
							object->SetSize({ 64, 512 });
							object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight-512 });
							object->SetStartPosition(object->GetPosition());
							object->SetObjectType(ObjectType::GROUND);
							objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;
						}
						else if (ColInit == "Scissors")
						{
							Scissors* object = new Scissors;
							object->SetSize({ 32, 64 });
							object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
							object->SetObjectType(ObjectType::Scissors);
							objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;
						}
						else
						{

							StaticObject* object = new StaticObject;
							object->SetImage(m_hTileSet);
							object->SetSize({ (float)m_nTileWidth, (float)m_nTileHeight });
							object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
							object->SetTexttureRectangle(CellAlgorithm(ID));

							
							 if (ColInit == "Fire Pit")
							{
								object->SetObjectType(ObjectType::DEADLY);

							}
							else if (ColInit == "Black Hole")
							{
								object->SetObjectType(ObjectType::DEADLY);
							}
							else if (ColInit == "Land Mine")
							{
								object->SetObjectType(ObjectType::LandMine);
								ParticleSystem ps;
								objects->AddObjectTable(object, 0);
								ps.Load("resource//savefiles//LandMineExpl.xml", ps, *object);
								object->SetParticle(ps);
								object->SetWasTriggered(false);
							}
							else if (ColInit == "Acid Pool")
							{
								object->SetObjectType(ObjectType::AcidPool);
								ParticleSystem ps;
								ps.Load("resource/savefiles/AcidBubbles.xml", ps, *object);
								object->SetParticle(ps);
							}
							else if (ColInit == "Tesla Coil")
							{
								object->SetObjectType(ObjectType::TeslaCoil);
							}
							else if (ColInit == "Poster")
							{
								object->SetObjectType(ObjectType::GROUND);
							}
							else if (ColInit == "Hanging Target")
							{
								object->SetObjectType(ObjectType::GROUND);

							}
							else if (ColInit == "Hanging Wire")
							{
								object->SetObjectType(ObjectType::Wire);
							}
							else if (ColInit == "Smashable")
							{

							}
							else if (ColInit == "Phaseable")
							{
								object->SetObjectType(ObjectType::PhaseWall);
							}
							else if (ColInit == "Hookable")
							{
								object->SetObjectType(ObjectType::Hookable);
							}
							else if (ColInit == "TopLeftSlant")
							{
								object->SetObjectType(ObjectType::TopLeftSlant);
								
							}
							else if (ColInit == "TopRightSlant")
							{
								object->SetObjectType(ObjectType::TopRightSlant);

							}
							else if (ColInit == "BottomLeftSlant")
							{
								object->SetObjectType(ObjectType::BottomLeftSlant);
							}
							else if (ColInit == "BottomRightSlant")
							{
								object->SetObjectType(ObjectType::BottomRightSlant);

							}
							else if (ColInit == "PhaseGauntlets")
							{
							}
							else if (ColInit == "Scissors")
							{
							}
							else if (ColInit == "Win")
							{
								object->SetObjectType(ObjectType::Win);

							}
							else if (ColInit == "InnerTopLeftSlant")
							{
								object->SetObjectType(ObjectType::InnerTopLeftSlant);

							}
							else if (ColInit == "InnerTopRightSlant")
							{
								object->SetObjectType(ObjectType::InnerTopRightSlant);

							}
							else if (ColInit == "InnerBottomLeftSlant")
							{
								object->SetObjectType(ObjectType::InnerBottomLeftSlant);

							}
							else if (ColInit == "InnerBottomRightSlant")
							{
								object->SetObjectType(ObjectType::InnerBottomRightSlant);

							}
							else if (ColInit == "Safe")
							{
								object->SetObjectType(ObjectType::Safe);
							}
							else if (ColInit == "GateUp")
							{
								object->SetObjectType(ObjectType::GateUp);
							}
							else if (ColInit == "NotSafe")
							{
								object->SetObjectType(ObjectType::NotSafe);
							}
							else if (ColInit == "FastPotion")
							{
								object->SetObjectType(ObjectType::FASTPOTION);
								objects->AddObjectTable(object, 0);
							}
							else if (ColInit == "SlowPotion")
							{
								object->SetObjectType(ObjectType::SLOWPOTION);
								objects->AddObjectTable(object, 0);
							}
							else if (ColInit == "NoDeathPotion")
							{
								object->SetObjectType(ObjectType::NODEATHPOTION);
							}
							else if (ColInit == "NoFrictionPotion")
							{
								object->SetObjectType(ObjectType::NOFRICTIONPOTION);
							}
							else if (ColInit == "PortalMenu")
							{
								object->SetObjectType(ObjectType::PortalMenu);
							}
							else if (ColInit == "PortalNext")
							{
								object->SetObjectType(ObjectType::PortalNext);
							}
							else if (ColInit == "PortalReset")
							{
								object->SetObjectType(ObjectType::PortalReset);
							}
							else if (ColInit == "Jet")
							{
								object->SetObjectType(ObjectType::Jet);
							}
							
							else if (ColInit == "PlayerStart")
							{
								GameplayState::GetInstance()->SetPlayerStart({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
							}
							else
								object->SetObjectType(ObjectType::GROUND);

							objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;
							//object->AddRef();

							// fake Layer
							//tempTile = Tile(X, Y, Lay, 0, ColID, ColInit, false);
							//tempLayer->m_arrTileArray[X + Y * m_nMapWidth] = tempTile;
						}
					}
					else
					{
						tempTile = new Tile(X, Y, Lay, ID, ColID, "None", false);
						m_vLayerVector.at(Lay)->m_arrTileArray[X + Y * m_nMapWidth] = tempTile;
						m_ndelete.push_back(X + Y * m_nMapWidth);
						m_ndeletelayer.push_back(Lay);
					}
		
		}
	}
	else 
		return false;

	//TiXmlDocument doc;

	//if (doc.LoadFile(FileName) == false)
	//	return false;

	//TiXmlElement* pTile_System = doc.RootElement();

	//if (pTile_System == nullptr)
	//	return false;

	//TiXmlElement *pTileID = pTile_System->FirstChildElement("Tile_Set_Image_ID");
	//m_szTileSetID = "resource/graphics/";
	//m_szTileSetID.append(pTileID->GetText());

	//Loading = L"Loading.. 2%";
	//font.Draw(&Loading, width, height, 1, SGD::Color());
	//pGraphics->Update();

	//TiXmlElement *pTileLayerNumber = pTileID->NextSiblingElement("Tile_Number_Of_Layers");
	//pTileLayerNumber->Attribute("LayerCount", &m_nTotalLayers);

	//Loading = L"Loading.. 4%";
	//font.Draw(&Loading, width, height, 1, SGD::Color());
	//pGraphics->Update();

	//TiXmlElement *pMapDimensions = pTileLayerNumber->NextSiblingElement("Map_Dimensions");
	//pMapDimensions->Attribute("MapWidth", &m_nMapWidth);
	//pMapDimensions->Attribute("MapHeight", &m_nMapHeight);

	//Loading = L"Loading.. 6%";
	//font.Draw(&Loading, width, height, 1, SGD::Color());
	//pGraphics->Update();

	//TiXmlElement *pTileDimensions = pMapDimensions->NextSiblingElement("Tile_Dimensions");
	//pTileDimensions->Attribute("TileWidth", &m_nTileWidth);
	//pTileDimensions->Attribute("TileHeight", &m_nTileHeight);

	//Loading = L"Loading.. 8%";
	//font.Draw(&Loading, width, height, 1, SGD::Color());
	//pGraphics->Update();

	//TiXmlElement *pTileRowandCol = pTileDimensions->NextSiblingElement("TileRowandCol");
	//pTileRowandCol->Attribute("TileRows", &m_nTileRows);
	//pTileRowandCol->Attribute("TileColumns", &m_nTileColumns);


	//TiXmlElement *pLayer = pTileDimensions->NextSiblingElement("Layer");


	//m_hTileSet = pGraphics->LoadTexture(m_szTileSetID.c_str());//L"resource/graphics/super4x4x64x64.png");

	//m_vLayerVector.clear();

	//// resize object vector to total number of tiles
	//objects->GetAllObjects()->resize(m_nMapWidth * m_nMapHeight);

	//float count = 1;

	//Loading = L"Loading.. 10%";
	//font.Draw(&Loading, width, height, 1, SGD::Color());
	//pGraphics->Update();

	//for (int i = 0; i < m_nTotalLayers; i++)
	//{
	//	TiXmlElement* pTiles = pLayer->FirstChildElement("Tile");

	//	Layer * tempLayer = new Layer;
	//	
	//	tempLayer->TotalTiles = m_nMapWidth * m_nMapHeight;
	//	tempLayer->m_arrTileArray.resize(m_nMapWidth * m_nMapHeight);
	//	int X, Y, Lay, ID, ColID,IsCol;

	//	std::string ColInit;

	//	int er = Loading.find_first_of('.');
	//	Loading.erase(Loading.begin() + er + 3, Loading.end());

	//	if (i == 1)
	//	{
	//		//Loading += to_wstring(i * 40);
	//		Loading += L"60%";
	//		font.Draw(&Loading, width, height, 1, SGD::Color());
	//		pGraphics->Update();
	//	}
	//	else if (i == 2)
	//	{
	//		//Loading += to_wstring(i * 40);
	//		Loading += L"80%";
	//		font.Draw(&Loading, width, height, 1, SGD::Color());
	//		pGraphics->Update();
	//	}

	//	while (pTiles != nullptr)
	//	{
	//		//Loading += to_wstring(int(count/(tempLayer->TotalTiles*m_nTotalLayers)*100.f));
	//		//Loading += L"%";
	//		//font.Draw(&Loading, width, height, 1, SGD::Color());
	//		//pGraphics->Update();
	//		//count++;

	//		//int er = Loading.find_first_of('.');
	//		//Loading.erase(Loading.begin() + er + 3, Loading.end());

	//		pTiles->Attribute("locX", &X);
	//		pTiles->Attribute("locY", &Y);
	//		pTiles->Attribute("Layer", &Lay);
	//		pTiles->Attribute("TileID", &ID);
	//		pTiles->Attribute("CollisionTrigger", &ColID);
	//		ColInit = pTiles->Attribute("TriggerInit");
	//		pTiles->Attribute("IsCollidable", &IsCol);

	//		Tile tempTile;
	//		// If there is collision, create an object, if not, create a tile
	//		if (IsCol)
	//		{
	//			if (ColInit == "Lights")
	//			{
	//				EmergencyLights* object = new EmergencyLights;
	//				object->SetSize({ 32, 64 });
	//				object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
	//				object->SetObjectType(ObjectType::GROUND);
	//				objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;

	//			}
	//			else if(ColInit == "SmashCrate")
	//			{
	//				Crate* crate = new Crate;
	//				crate->SetSize({128, 128});
	//				crate->SetPosition({(float)X * m_nTileWidth, (float)Y * m_nTileHeight});
	//				crate->SetObjectType(ObjectType::SmashCrate);
	//				objects->GetAllObjects()->at(X + Y *m_nMapWidth) = crate;
	//				objects->AddObjectTable(crate, 0);
	//			}

	//			else if (ColInit == "GravBoots")
	//			{
	//				Boots* object = new Boots;
	//				object->SetSize({ 64, 64 });
	//				object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
	//				object->SetObjectType(ObjectType::GravBoots);
	//				objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;
	//			}
	//			else if (ColInit == "Gate")
	//			{
	//				Gate* object = new Gate;
	//				object->SetSize({ 64, 512 });
	//				object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight-512 });
	//				object->SetStartPosition(object->GetPosition());
	//				object->SetObjectType(ObjectType::GROUND);
	//				objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;
	//			}
	//			else if (ColInit == "Scissors")
	//			{
	//				Scissors* object = new Scissors;
	//				object->SetSize({ 32, 64 });
	//				object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
	//				object->SetObjectType(ObjectType::Scissors);
	//				objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;
	//			}
	//			else
	//			{

	//				StaticObject* object = new StaticObject;
	//				object->SetImage(m_hTileSet);
	//				object->SetSize({ (float)m_nTileWidth, (float)m_nTileHeight });
	//				object->SetPosition({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
	//				object->SetTexttureRectangle(CellAlgorithm(ID));

	//				
	//				if (ColInit == "Ceiling Fan")
	//				{
	//					object->SetObjectType(ObjectType::Fan);
	//				}
	//				else if (ColInit == "Fire Pit")
	//				{
	//					object->SetObjectType(ObjectType::DEADLY);

	//				}
	//				else if (ColInit == "Black Hole")
	//				{
	//					object->SetObjectType(ObjectType::DEADLY);
	//				}
	//				else if (ColInit == "Land Mine")
	//				{
	//					object->SetObjectType(ObjectType::LandMine);
	//					ParticleSystem ps;
	//					objects->AddObjectTable(object, 0);
	//					ps.Load("resource//savefiles//LandMineExpl.xml", ps, *object);
	//					object->SetParticle(ps);
	//					object->SetWasTriggered(false);
	//				}
	//				else if (ColInit == "Acid Pool")
	//				{
	//					object->SetObjectType(ObjectType::AcidPool);
	//					ParticleSystem ps;
	//					ps.Load("resource/savefiles/AcidBubbles.xml", ps, *object);
	//					object->SetParticle(ps);
	//				}
	//				else if (ColInit == "Tesla Coil")
	//				{
	//					object->SetObjectType(ObjectType::TeslaCoil);
	//				}
	//				else if (ColInit == "Poster")
	//				{
	//					object->SetObjectType(ObjectType::GROUND);
	//				}
	//				else if (ColInit == "Hanging Target")
	//				{
	//					object->SetObjectType(ObjectType::GROUND);

	//				}
	//				else if (ColInit == "Hanging Wire")
	//				{
	//					object->SetObjectType(ObjectType::GROUND);
	//				}
	//				else if (ColInit == "Smashable")
	//				{

	//				}
	//				else if (ColInit == "Phaseable")
	//				{
	//					object->SetObjectType(ObjectType::PhaseWall);
	//				}
	//				else if (ColInit == "Hookable")
	//				{
	//					object->SetObjectType(ObjectType::Hookable);
	//				}
	//				else if (ColInit == "TopLeftSlant")
	//				{
	//					object->SetObjectType(ObjectType::TopLeftSlant);
	//					
	//				}
	//				else if (ColInit == "TopRightSlant")
	//				{
	//					object->SetObjectType(ObjectType::TopRightSlant);

	//				}
	//				else if (ColInit == "BottomLeftSlant")
	//				{
	//					object->SetObjectType(ObjectType::BottomLeftSlant);
	//				}
	//				else if (ColInit == "BottomRightSlant")
	//				{
	//					object->SetObjectType(ObjectType::BottomRightSlant);

	//				}
	//				else if (ColInit == "GrappleHook")
	//				{
	//					object->SetObjectType(ObjectType::GRAPPLEHOOK);
	//				}
	//				else if (ColInit == "PhaseGauntlets")
	//				{
	//				}
	//				else if (ColInit == "Scissors")
	//				{
	//				}
	//				else if (ColInit == "Win")
	//				{
	//					object->SetObjectType(ObjectType::Win);

	//				}
	//				else if (ColInit == "InnerTopLeftSlant")
	//				{
	//					object->SetObjectType(ObjectType::InnerTopLeftSlant);

	//				}
	//				else if (ColInit == "InnerTopRightSlant")
	//				{
	//					object->SetObjectType(ObjectType::InnerTopRightSlant);

	//				}
	//				else if (ColInit == "InnerBottomLeftSlant")
	//				{
	//					object->SetObjectType(ObjectType::InnerBottomLeftSlant);

	//				}
	//				else if (ColInit == "InnerBottomRightSlant")
	//				{
	//					object->SetObjectType(ObjectType::InnerBottomRightSlant);

	//				}
	//				else if (ColInit == "Safe")
	//				{
	//					object->SetObjectType(ObjectType::Safe);
	//				}
	//				else if (ColInit == "NotSafe")
	//				{
	//					object->SetObjectType(ObjectType::NotSafe);
	//				}
	//				else if (ColInit == "FastPotion")
	//				{
	//					object->SetObjectType(ObjectType::FASTPOTION);
	//					objects->AddObjectTable(object, 0);
	//				}
	//				else if (ColInit == "SlowPotion")
	//				{
	//					object->SetObjectType(ObjectType::SLOWPOTION);
	//					objects->AddObjectTable(object, 0);
	//				}
	//				else if (ColInit == "NoDeathPotion")
	//				{
	//					object->SetObjectType(ObjectType::NODEATHPOTION);
	//				}
	//				else if (ColInit == "NoFrictionPotion")
	//				{
	//					object->SetObjectType(ObjectType::NOFRICTIONPOTION);
	//				}
	//				else if (ColInit == "PortalMenu")
	//				{
	//					object->SetObjectType(ObjectType::PortalMenu);
	//				}
	//				else if (ColInit == "PortalNext")
	//				{
	//					object->SetObjectType(ObjectType::PortalNext);
	//				}
	//				else if (ColInit == "PlayerStart")
	//				{
	//					GameplayState::GetInstance()->SetPlayerStart({ (float)X * m_nTileWidth, (float)Y * m_nTileHeight });
	//				}
	//				else
	//					object->SetObjectType(ObjectType::GROUND);

	//				objects->GetAllObjects()->at(X + Y * m_nMapWidth) = object;
	//				//object->AddRef();

	//				// fake Layer
	//				//tempTile = Tile(X, Y, Lay, 0, ColID, ColInit, false);
	//				//tempLayer->m_arrTileArray[X + Y * m_nMapWidth] = tempTile;
	//			}
	//		}
	//		else
	//		{
	//			tempTile = Tile(X, Y, Lay, ID, ColID, ColInit, false);
	//				tempLayer->m_arrTileArray[X + Y * m_nMapWidth] = tempTile;
	//		}

	//		pTiles = pTiles->NextSiblingElement("Tile");

	//	}
	//	m_vLayerVector.push_back(tempLayer);
	//	pLayer = pLayer->NextSiblingElement("Layer");
	//	//TiXmlElement *pTiles = pLayer->FirstChildElement("Tile");
	//	//Layer tempLayer;
	//	//tempLayer.TotalTiles = 0;
	//	//tempLayer.m_arrTileArray = new Tile[m_nMapHeight * m_nMapWidth];
	//	//
	//	//int X, Y, Lay, ID, ColID, IsCol;
	//	//std::string ColInit;
	//	//int count = 0;
	//	//
	//	//while (pTiles != nullptr)
	//	//{
	//	//	pTiles->Attribute("locX", &X);
	//	//	pTiles->Attribute("locY", &Y);
	//	//	pTiles->Attribute("Layer", &Lay);
	//	//	pTiles->Attribute("TileID", &ID);
	//	//	pTiles->Attribute("CollisionTrigger", &ColID);
	//	//	ColInit = pTiles->Attribute("TriggerInit");
	//	//	pTiles->Attribute("IsCollidable", &IsCol);
	//	//
	//	//	Tile tempTile = Tile(X, Y, Lay, ID, ColID, ColInit, IsCol);
	//	//
	//	//
	//	//	//tempLayer.m_arrTileArray[X + (Y * m_nMapWidth)] = tempTile;
	//	//	tempLayer.m_arrTileArray[count] = tempTile;
	//	//
	//	//	pTiles = pTiles->NextSiblingElement("Tile");
	//	//	tempLayer.TotalTiles++;
	//	//	//m_vTileVector[tempTile.m_nLocationY].push_back(tempTile);
	//	//	count++;
	//	//}
	//	//m_vLayerVector.push_back(tempLayer);
	//	//pLayer = pLayer->NextSiblingElement("Layer");
	//	//

	//}

	//int er = Loading.find_first_of('.');
	//Loading.erase(Loading.begin() + er + 3, Loading.end());
	//Loading += L"100%";
	//font.Draw(&Loading, width, height, 1, SGD::Color());
	//pGraphics->Update();
	return (m_vLayerVector.size() > 0);
}

void TileSystem::Save(const char* FileName)
{
	TiXmlDocument doc;

	TiXmlDeclaration* pDecl = new TiXmlDeclaration{ "1.0", "utf-8", "" };

	doc.LinkEndChild(pDecl);

	TiXmlElement* pTile_System = new TiXmlElement{ "Tile_System" };

	doc.LinkEndChild(pTile_System);

	//The Tile ID element
	TiXmlElement* pTileID = new TiXmlElement{ "Tile_Set_Image_ID" };

	TiXmlText* pText = new TiXmlText{ "../resource/graphics/Background_Wall.png" };

	pTileID->LinkEndChild(pText);

	pTile_System->LinkEndChild(pTileID);
	//The Layer element
	TiXmlElement *pTileLayerNumber = new TiXmlElement{ "Tile_Number_Of_Layers" };

	pTileLayerNumber->SetAttribute("LayerCount", 1);

	pTile_System->LinkEndChild(pTileLayerNumber);

	TiXmlElement *pMapDimensions = new TiXmlElement{ "Map_Dimensions" };

	pMapDimensions->SetAttribute("MapWidth", 4);
	pMapDimensions->SetAttribute("MapHeight", 4);

	pTile_System->LinkEndChild(pMapDimensions);

	TiXmlElement *pTileDimensions = new TiXmlElement{ "Tile_Dimensions" };

	pTileDimensions->SetAttribute("TileWidth", 64);
	pTileDimensions->SetAttribute("TileHeight", 64);

	pTile_System->LinkEndChild(pTileDimensions);

	TiXmlElement *pLayer = new TiXmlElement{ "Layer" };

	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			TiXmlElement *pTile = new TiXmlElement{ "Tile" };
			pTile->SetAttribute("locX", i);
			pTile->SetAttribute("locY", j);
			pTile->SetAttribute("Layer", 0);
			pTile->SetAttribute("TileID", i + j);
			pTile->SetAttribute("CollisionTrigger", 0);
			pTile->SetAttribute("TriggerInit", "None");
			pTile->SetAttribute("IsCollidable", false);

			pLayer->LinkEndChild(pTile);
		}

	}
	pTile_System->LinkEndChild(pLayer);

	doc.SaveFile(FileName);
}
bool TileSystem::CheckCol(BaseObject* pOther)
{
	return true;
}